<?php

class student extends IgnitedRecord
{
	var $has_and_belongs_to_many = 'parentts';
	var $belongs_to = array('classe', 'login');
	var $has_many = array('attendances', 'grades');
	
	function student()
	{
        parent::IgnitedRecord();
    }
    
    function get()
    {
        $query = $this->db->query(	"SELECT s.id, s.firstname, s.secondname, c.name, ".
                                  	"s.birth, s.email FROM students s, classes c ".
    								"WHERE c.id = s.classe_id ");
        $count = $this->db->count_all('students');
    	$heading = array('ID', 'Imię', 'Nazwisko', 'Klasa', 'Data urodzenia', 'E-mail');
    	
    	return array('query'=> $query, 'heading' => $heading, 'count' => $count);    	
    }
    
    function update($_POST, $id)
    {
        $this->db->where('id', $id);
        
        $val['firstname'] = $_POST['firstname'];
        $val['secondname'] = $_POST['secondname'];
        $val['birth'] = $_POST['birth'];
        $val['email'] = $_POST['email'];
        $val['classe_id'] = $_POST['classe_id'];
        $this->db->update('students', $val);
    }
    
    function get_related($id)
    {
        $this->db->where('id', $id);
        foreach ($this->db->get('students')->result() as $row):
            $related['classe_id'] = $row->classe_id;
        endforeach;
        
        $this->db->where('id', $related['classe_id']);
        foreach ($this->db->get('classes')->result() as $row):
            $related['classe'] = $row->name;
        endforeach;
        
        return $related;
    }
}
?>
